#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

//p1
int min(int x, int y) { return x > y ? x : y; }

//p2
void chline(char ch, int i, int j);

//p3 同二
//p4 求调和平均数
double avg_th(double v1, double v2);

//p5
void large_of(double *x, double *y);

//p6
void sort(double *x, double *y, double *z);

//p7.判断输入流中的字符是否是字母若是打印位置
int position_alpha(char ch);

void reportCharSteam();

//p8.计算幂
double power(double x, int y);

void test_power();

//p9.p8递归版
double power1(double x, int y);

//p10.将10进制转换为任意进制
void to_base_n(int n, int scale);
void to_base_n1(int n, int scale);

//p11.用循环编写fibonacci
void fibonacci(int n);

int main() {
    double d1 = 2.3, d2 = 7.2, d3 = 4.2;
    //p1(d1,d2);
//    min(d1,d2);
    //p2p3
//    chline('a', 10, 20);
    //p4
//    printf("%f\n", avg_th(d1, d2));
//    //p5
//    printf("%f,%f\n", d1, d2);
//    large_of(&d1, &d2);
//    printf("%f,%f\n", d1, d2);
    //p6
//    printf("%f,%f,%f\n", d1, d2, d3);
//    sort(&d1,&d2,&d3);
//    printf("%f,%f,%f\n", d1, d2, d3);
    //p7
//    reportCharSteam();
    //p8p9
//    test_power();
    //p10
    to_base_n(300, 8);
//    to_base_n1(300, 8);
    //p11
//    fibonacci(10);
    return 0;
}

void chline(char ch, int i, int j) {
    for (int row = 0; row < i; row++) {
        for (int col = 0; col < j; col++) {
            printf("c");
        }
        printf("\n");
    }
}

double avg_th(double v1, double v2) {
    return (1 / v1 + 1 / v2) / 2;
}

void large_of(double *x, double *y) {
//    double large = *x;
//    if (*y > large) {
//        large = *y;
//    }
//    *x = large;
//    *y = large;
    *x = *y = *x > *y ? *x : *y;
}

void sort(double *x, double *y, double *z) {
    double max = *x;
    double min = *x;
    double mid = *x;
    if (*y > max) {
        max = *y;
    }
    if (*z > max) {
        max = *z;
    }
    if (*y < min) {
        max = *y;
    }
    if (*z < min) {
        max = *z;
    }
    if (*z != max && *z != min) {
        mid = *z;
    }
    if (*y != max && *y != min) {
        mid = *y;
    }
    *x = min;
    *y = mid;
    *z = max;
}

int position_alpha(char ch) {
    char c = toupper(ch);
    if ('A' <= c && 'Z' >= c) {
        return c - 'A' + 1;
    } else {
        return -1;
    }
}

void reportCharSteam() {
    char ch;
    while ((ch = getchar()) != EOF) {
        printf("%d ", position_alpha(ch));
    }
}

double power(double x, int y) {
    double result = 1;
    if (x == 0) {
        if (y == 0) {
            printf("0的0次幂未定义。\n");
        } else {
            result = 0;
        }
    } else {
        if (y > 0) {
            for (int i = 0; i < y; ++i) {
                result *= x;
            }
        } else if (y < 0) {
            for (int i = 0; i < -y; ++i) {
                result *= 1 / x;
            }
        }
    }
    return result;
}

double power1(double x, int y) {
    if (x == 0) {
        if (y == 0) {
            printf("0的0次幂未定义。\n");
            return 1;
        } else {
            return 0;
        }
    } else {
        if (y > 0) {
            return x * (power(x, y - 1));
        } else if (y < 0) {
            return x * (power(x, y + 1));
        } else {
            return 1;
        }
    }
}

void test_power() {
    for (double i = -2; i <= 2; i += .5) {
        for (int j = -2; j <= 2; ++j) {
            printf("%.3f的%d次幂等于%.3f\n", i, j, power1(i, j));
        }
    }
}

void to_base_n(int n, int scale) {
    if (n / scale > 0) {
        to_base_n(n / scale, scale);
    }
    putchar('0' + n % scale);
}

//参考答案有问题
void to_base_n1(int x, int base) {/* recursive function */
    int r;
    r = x % base;
    if (x >= base)
        to_base_n(x / base, base);
    putchar('0' + r);
}
void fibonacci(int n) {
    int np1 = 1;
    int np2 = 1;
    for (int i = 1; i <= n; ++i) {
        if (i <= 2) {
            printf("1 ");
        } else if (i == 3) {
            printf("2 ");
            np2 = 2;
        } else {
            printf("%d ", np1 + np2);
            int temp = np2;
            np2 += np1;
            np1 = temp;
        }
    }
}